package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.SetMealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.SetmealVO;
import lombok.SneakyThrows;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@Service
@Transactional
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {
    @Autowired
    private CheckGroupService checkGroupService;
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Override
    public void add(SetmealDTO setmealDTO) {
        Object o = RedisUtil.get(RedisConstant.SINGLE_PIC + setmealDTO.getImg());
        if (o==null&&setmealDTO.getId()==null){
            throw new RuntimeException("图片超时，请重新提交");
        }
        //套餐添加
        saveOrUpdate(setmealDTO);
        Integer id = setmealDTO.getId();
        //删除中间表
        baseMapper.deleteGroup(id);
        //绑定检查组
        Integer[] checkgroupIds = setmealDTO.getCheckgroupIds();
        for (Integer groupId : checkgroupIds) {
            baseMapper.addSetMealAndCheckGroupInfos(id,groupId);
        }
    }

    @Override
    public Map findPage(QueryPageBean queryPageBean) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        queryWrapper.like("name",queryPageBean.getQueryString()==null?"":queryPageBean.getQueryString());
        Page<Setmeal> page = page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        Map map=new HashMap();
        map.put("list",page.getRecords());
        map.put("total",page.getTotal());
        return map;
    }

    @Override
    public SetmealVO findGroupById(Integer id) {
        SetmealVO setmealVO = new SetmealVO();
        setmealVO.setCheckGroupList(checkGroupService.findAll());
        Integer[] ids = baseMapper.findGroupById(id);
        setmealVO.setCheckgroupIds(ids);
        return setmealVO;
    }

    /**
     * 该方法由xxl-job框架调用
     */
    @Override
    public void clearOssObject() {
        //    清理  oss  垃圾图片   该方法 有  xxl-job 去调用
        //  1.  获取 set 集合 所有图片信息
        Set<String> members = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        if (members.size() != 0) {
            for (String uuidfilename : members) {
                //   遍历 图片  判断 redis 该图片有没有存在
                boolean existsKey = RedisUtil.existsKey(RedisConstant.SINGLE_PIC + uuidfilename);
                if (existsKey == false) {
                    //   set集合中没找到  redis 对应得图片信息  那么通知 oss删除图片即可
                    AliyunUtils.deleteFile(uuidfilename);
                    System.out.println("-----阿里云删除垃圾图片一次----");
                    //  set    集合  垃圾图片 删除
                    RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET, uuidfilename);
                }
            }
        }
    }

    @SneakyThrows
    @Override
    public List<Setmeal> findAll() {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        return list(queryWrapper);
    }

    @Override
    public List<Setmeal> findHot() {
        List<Setmeal> hot = baseMapper.findHot();
        RedisUtil.set("HotSetMeal",hot);
        return hot;
    }

    @Override
    public Setmeal findSetMealDetail(Integer id) {
        //根据id查询套餐
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("is_delete",0);
        Setmeal setmeal = baseMapper.selectOne(queryWrapper);
        //根据id获取检查组
        List<CheckGroup> checkGroupList = baseMapper.findCheckGroupsBySetmealId(id);
        //获取检查组所绑定的检查项
        for (CheckGroup checkGroup : checkGroupList) {
            List<CheckItem> checkItemList = checkGroupMapper.findCheckItemsByGroupId(checkGroup.getId());
            checkGroup.setCheckItems(checkItemList);
        }
        setmeal.setCheckGroups(checkGroupList);
        return setmeal;
    }

    @Override
    public Setmeal findSetMealById(Integer id) {
        //根据id查询套餐
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("is_delete",0);
        Setmeal setmeal = baseMapper.selectOne(queryWrapper);
        return setmeal;
    }

    @Override
    public List<Map> getSetmealProportion() {
        return baseMapper.getSetmealProportion();
    }
}
